package com.runen.infinitemovement.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.activity.InviteFriendsActivity;
import com.runen.infinitemovement.activity.MainActivity;
import com.runen.infinitemovement.activity.StepsForActivity;
import com.runen.infinitemovement.model.entity.MZBannerEntity;
import com.runen.infinitemovement.model.enums.MZBannerType;
import com.youth.banner.adapter.BannerAdapter;

import java.util.List;

/**
 * Create by LiaoDuanHong
 * Create on
 */
public class MyBannerAdapter extends BannerAdapter<MZBannerEntity, MyBannerAdapter.BannerViewHolder> {
    private TextView tv_title, tv_describe;
    private ImageView iv_ico;
    private Context context;

    public MyBannerAdapter(Context context, List<MZBannerEntity> datas) {
        super(datas);
        this.context = context;
    }

    public void updateData(List<MZBannerEntity> datas) {
        if (datas != null) {
            mDatas.clear();
            mDatas.addAll(datas);
            notifyDataSetChanged();
        }
    }

    @Override
    public BannerViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.banner_home, null);
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        view.setLayoutParams(lp);
        tv_title = view.findViewById(R.id.tv_title);
        tv_describe = view.findViewById(R.id.tv_describe);
        iv_ico = view.findViewById(R.id.iv_ico);
        return new BannerViewHolder(view);
    }

    @Override
    public void onBindView(BannerViewHolder holder, MZBannerEntity data, int position, int size) {
        switch (data.getType()) {
            case MZBannerType.STEP_COUNT:
                String text = "步数兑换<font color='#ed3f37'>%s</font>步";
                tv_title.setText(Html.fromHtml(String.format(text, data.getStepCount())));
                tv_describe.setText("走的每一步都有意义");
                iv_ico.setImageResource(R.mipmap.ico_banner_1);
                break;
            case MZBannerType.SHARE_FRIENDS:
                String text1 = "邀请好友<font color='#ed3f37'>送火种</font>";
                tv_title.setText(Html.fromHtml(text1));
                tv_describe.setText("和小伙伴一起重走长征路");
                iv_ico.setImageResource(R.mipmap.ico_banner_2);
                break;
            case MZBannerType.SPORT:
                tv_title.setText("用运动重走长征路");
                tv_describe.setText("为每一次运动赋能");
                iv_ico.setImageResource(R.mipmap.ico_banner_3);
                break;
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (data.getType()) {
                    case MZBannerType.STEP_COUNT:
                        context.startActivity(new Intent(context, StepsForActivity.class));
                        break;
                    case MZBannerType.SHARE_FRIENDS:
                        context.startActivity(new Intent(context, InviteFriendsActivity.class));
                        break;
                    case MZBannerType.SPORT:
                        ((MainActivity) context).jumpToYundong();
                        break;
                }
            }
        });
    }

    public class BannerViewHolder extends RecyclerView.ViewHolder {
        public BannerViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
